package com.htc.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.htc.model.Company;
import com.htc.model.OrderComment;
import com.htc.model.OrderItem;
import com.htc.model.Product;
import com.htc.model.ProductComment;

public interface OrderItemMapper{

	@Insert("insert into orderitem(oid,pid,buynum) values(#{oid},#{pid},#{buynum})")
	public int saveOrderItem(OrderItem orderItem);

	@Delete("delete from orderitem where itemid=#{itemid}")
	public int deleteOrderItem(int itemid);

	@Update("update orderitem set oid=#{oid},pid=#{pid},buynum=#{buynum} where itemid=#{itemid}")
	public int updateOrderItem(OrderItem orderItem);

	@Select("select * from orderitem where itemid=#{itemid}")
	public OrderItem findOrderItemByItemid(int itemid);

	@Select("select * from orderitem")
	public List<OrderItem> findAllOrderItem();
	
	@Select("select * from orderitem where oid=#{oid}")
	public List<OrderItem> findOrderItemByOid(int oid);

	
	@Update("update orderitem set state=1,score=#{score},comment=#{comment},comment_time=now() where itemid=#{itemid} and state=0")
	public void updateComment(OrderItem orderItem);
	
	@Select("select consumer.*,count(buynum) buynum,AVG(score) socre from `order`,orderitem,consumer,product where `order`.oid=orderitem.oid and `order`.state=3 and orderitem.pid=product.pid and product.consumer_id=consumer.id and product.consumer_id=#{consumerId}")
	public Company queryCompany(int consumerId);
	
	@Select("select `comment`,comment_time,(select cellphone from consumer,`order` where consumer.id=`order`.uid and `order`.oid=orderitem.oid)cellphone from orderitem where `comment` is not null and pid=#{pid}")
	public List<OrderComment> queryProductComment(@Param("pid")int pid);
}
